﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.views.popups.adopt {
    import reprise.ui.*;
    import flash.events.*;
    import reprise.external.*;
    import flash.display.*;
    import reprise.controls.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.views.popups.*;
    import net.wooga.pets.models.world.vos.actors.*;
    import net.wooga.pets.constants.*;
    import net.wooga.pets.utils.*;
    import net.wooga.pets.models.world.*;
    import net.wooga.pets.external.*;

    public class AdoptBabyPopupMediator extends AbstractPopupMediator {

        public var friendsModel:FriendsModel;
        public var userModel:UserModel;
        public var world:WorldModel;
        private var _content:UIComponent;
        private var _babyVO:BabyVO;
        private var _mom:UIComponent;
        private var _dad:UIComponent;

        private static const EMPTY:String = "empty";
        private static const QUESTION_MARK:String = "questionMark";
        private static const MOM:String = "mom";
        private static const DAD:String = "dad";

        override public function onRegister():void{
            super.onRegister();
            addContextListener(EventConstants.CLOSE_POPUP_BY_TYPE, this.onClosePopup, InfoEvent);
            this._babyVO = (this.world.getActor(this.view.data.id) as BabyVO);
            addLabel("title showcardGothicFont", "kindergarten_find_family_header");
            this._content = RepriseUtils.addComponent(this.view, "content");
            RepriseUtils.addComponent(this._content, "familyTree");
            var _local1:MovieClip = AssetsLibrary.getMovieClip((this._babyVO.type + "Baby"));
            _local1.animation.gotoAndStop(GameConstants.ANIMATION_STATE_HAPPY);
            _local1.scaleX = -0.6;
            _local1.scaleY = 0.6;
            RepriseUtils.addClipComponent(this._content, "baby", _local1);
            this._mom = RepriseUtils.addComponent(this._content, "godparents mom");
            RepriseUtils.addComponent(this._mom, "imgBackground");
            RepriseUtils.addLabel(this._mom, "parentsLabel", language.getTextByKey("kindergarten_find_family_mom"));
            this._dad = RepriseUtils.addComponent(this._content, "godparents dad");
            RepriseUtils.addComponent(this._dad, "imgBackground");
            RepriseUtils.addLabel(this._dad, "parentsLabel", language.getTextByKey("kindergarten_find_family_dad"));
            this.renderGodMother();
            this.renderGodFather();
            this.renderButtons();
        }
        private function renderGodMother():void{
            var _local2:LabelButton;
            var _local1:MovieClip = AssetsLibrary.getMovieClip("RC_ImageBackground");
            if (this._babyVO.momId == GameConstants.KARLA_FACEBOOK_ID){
                _local1.gotoAndStop(MOM);
                RepriseUtils.addClipComponent(this._mom, "parentsImage", _local1);
            } else {
                if (this._babyVO.momId != null){
                    _local1.gotoAndStop(EMPTY);
                    RepriseUtils.addClipComponent(this._mom, "parentsImage", _local1);
                    this.addFBImage(this._babyVO.momId, this._mom);
                } else {
                    if (this._babyVO.momId == null){
                        _local1.gotoAndStop(QUESTION_MARK);
                        _local2 = RepriseUtils.addLabelButton(this._mom, "blueButton momButton", language.getTextByKey("global_button_hire"), this.onBuy);
                        _local2.setData({id:ProtocolConstants.GOD_MOM});
                        RepriseUtils.addComponent(_local2, "fbCreditsIcon");
                        RepriseUtils.addClipComponent(this._mom, "parentsImage", _local1);
                    };
                };
            };
        }
        private function renderGodFather():void{
            var _local2:LabelButton;
            var _local1:MovieClip = AssetsLibrary.getMovieClip("RC_ImageBackground");
            if (this._babyVO.dadId == GameConstants.KARLA_FACEBOOK_ID){
                _local1.gotoAndStop(DAD);
                RepriseUtils.addClipComponent(this._dad, "parentsImage", _local1);
            } else {
                if (this._babyVO.dadId != null){
                    _local1.gotoAndStop(EMPTY);
                    this.addFBImage(this._babyVO.dadId, this._dad);
                } else {
                    if (this._babyVO.dadId == null){
                        _local1.gotoAndStop(QUESTION_MARK);
                        _local2 = RepriseUtils.addLabelButton(this._dad, "blueButton dadButton", language.getTextByKey("global_button_hire"), this.onBuy);
                        _local2.setData({id:ProtocolConstants.GOD_FATHER});
                        RepriseUtils.addComponent(_local2, "fbCreditsIcon");
                        RepriseUtils.addClipComponent(this._dad, "parentsImage", _local1);
                    };
                };
            };
        }
        private function renderButtons():void{
            if (((!((this._babyVO.momId == null))) && (!((this._babyVO.dadId == null))))){
                RepriseUtils.addLabelButton(this._content, "greenButton sendToFamily", language.getTextByKey("kindergarten_find_family_button_send_family"), this.onSendToFamily);
            } else {
                RepriseUtils.addLabelButton(this._content, "greenButton askButton", language.getTextByKey("global_button_askfriends"), this.onAsk);
            };
        }
        private function onSendToFamily(_arg1:Event):void{
            dispatch(new InfoEvent(EventConstants.SEND_TO_FAMILY, null, this._babyVO));
            close();
        }
        private function onBuy(_arg1:Event):void{
            var _local2:LabelButton = (_arg1.currentTarget as LabelButton);
            dispatch(new InfoEvent(EventConstants.BUY_ADOPT, _local2.data().id, this._babyVO.id));
        }
        private function onAsk(_arg1:Event):void{
            var _local2:String = this._babyVO.id;
            var _local3:Object = {request_type:ProtocolConstants.ADOPT, is_gift:false, entity_id:_local2};
            dispatch(new InfoEvent(EventConstants.SEND_GIFT_REQUEST, null, _local3));
            close();
        }
        private function addFBImage(_arg1:String, _arg2:UIComponent):void{
            var _local3:String = GameConstants.FACEBOOK_DEFAULT_PIC;
            var _local4:int;
            while (_local4 < this.friendsModel.friends.length) {
                if (this.friendsModel.friends[_local4].uid == _arg1){
                    _local3 = this.friendsModel.friends[_local4].picture;
                    break;
                };
                _local4++;
            };
            var _local5:FacebookImageResource = new FacebookImageResource(_local3);
            _local5.addEventListener(Event.COMPLETE, this.imageResourceComplete);
            _local5.execute();
            RepriseUtils.addClipComponent(_arg2, "parentsImage", _local5.loader());
        }
        private function imageResourceComplete(_arg1:Event):void{
            var _local2:ImageResource = ImageResource(_arg1.target);
            _local2.removeEventListener(Event.COMPLETE, this.imageResourceComplete);
        }
        private function onClosePopup(_arg1:InfoEvent):void{
            if (_arg1.id == GameConstants.ADOPT_POPUP){
                close();
            };
        }
        private function get view():AdoptBabyPopupView{
            return ((viewComponent as AdoptBabyPopupView));
        }

    }
}//package net.wooga.pets.views.popups.adopt 
